﻿using AutoFlatnessTest.Db;
using AutoFlatnessTest.Plc;
using AutoFlatnessTest.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AutoFlatnessTest
{
    /// <summary>
    /// MainStartWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainStartWindow : Window
    {
        MainStartWindowVModel vModel = new MainStartWindowVModel();
        public bool isUserClose = false;
        string err = "";

        public MainStartWindow()
        {
            InitializeComponent();
            this.DataContext = vModel;
        }

        static bool islie = true;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _ = Task.Run(() =>
            {

                vModel.Txt = "正在初始化数据...";
                try
                {
                    //TestRecordDb.Ini();

                    using (var connection = DbHelp.GetSQLiteDataCenterConn())
                    {

                    }
                }
                catch (Exception ex)
                {
                    vModel.Txt = "初始化数据失败，请联系管理员。";
                    vModel.IsClose = true;
                    err = ex.ToString();
                    return;
                }
                Thread.Sleep(100);

                vModel.Txt = "正在检查文件...";
                if (!File.Exists(LocalFile.DbPath) || !File.Exists(LocalFile.ConPath))
                {
                    vModel.Txt = "缺少主要文件。";
                    vModel.IsClose = true;
                    err = "缺少主要文件。";
                    return;
                }
                Thread.Sleep(100);

                vModel.Txt = "正在连接设备...";
                try
                {
                    //测试时可以注释此句话
                    if (islie)
                        InovanceRun.Open();
                }
                catch (Exception ex)
                {
                    vModel.Txt = "连接设备失败，请检查网线或联系管理员。";
                    vModel.IsClose = true;
                    err = ex.ToString();
                    return;
                }
                Thread.Sleep(100);

                vModel.Txt = "正在连接扫码枪...";
                try
                {
                    //测试时可以注释此句话
                    if (islie)
                        JiEnShiRun.OpenSma();
                }
                catch (Exception ex)
                {
                    vModel.Txt = "连接设备失败，请检查网线或联系管理员。";
                    vModel.IsClose = true;
                    err = ex.ToString();
                    return;
                }
                Thread.Sleep(100);


                vModel.Txt = "正在连接扫码仪...";
                try
                {
                    //测试时可以注释此句话
                    if (islie)
                        JiEnShiRun.OpenSmi();
                }
                catch (Exception ex)
                {
                    vModel.Txt = "连接设备失败，请检查网线或联系管理员。";
                    vModel.IsClose = true;
                    err = ex.ToString();
                    return;
                }
                Thread.Sleep(100);

                this.Dispatcher.Invoke(() =>
                {
                    this.Close();
                });
            });
        }

        private void but_close(object sender, RoutedEventArgs e)
        {
            isUserClose = true;
            this.Close();
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(err))
                MessageBox.Show(err);
        }

    }
}
